
#include <iostream>
#include "Egg.h"

#include "Game.h"


Egg::Egg(Game *game, const char *pathImage, double scaleRate)
	:Object(game, pathImage,scaleRate) 
{
	setType(KindOfObject::kEgg);
	frameRateForFallingDown = FRAME_RATE_FOR_FALLING_DOWN;
	lastTimeForMoving = SDL_GetTicks();// Ham Thu Vien 
	setState(States::kStop);
}

void Egg::draw()
{
	switch(currentState)
	{
		case States::kFallDown:
			Object::draw();
			break;
		case States::kStop:			
			break;
	}
}

void Egg::update()
{
	double delta = (double)(SDL_GetTicks() - lastTimeForMoving ) / 1000 ;
	double timeLate = 1/ frameRateForFallingDown;
	if (delta >= timeLate)
	{
		switch(currentState)
		{
			case States::kFallDown:
				if (rect.sdlRect.y <= SCREEN_HEIGHT)
				{
					moveDown();
					if (checkColision((Object *) game->basket) )
					{
						game->increaseEggs(1);
						if (game->eggs >0 && game ->eggs % EGGS_FOR_EVERY_LEVEL == 0)
						{
							game->increaseLevels(1);
							game->increaseEggsSpeed();
						}
						setSpeed(game->speedForAllofEggs);
						setState(States::kStop);
					}
				}else
				{
					game->descreaseLives(1);
					setState(States::kStop);
					if (game->basket->life==0) game->setState(Game::States::kGameOver);
				}
				break;
			case States::kStop:
				setPositions(-1,-1);
				break;
		}
		lastTimeForMoving = SDL_GetTicks();
	}
	//std::cout << speed << "\n";
}